package com.ddzj.scaffold.utils;

import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;

/**
 * @Author:yangzibo
 * @CreateTime:2024-05-12 18:39
 * @Description:
 * @Version:1.0
 **/
public class RSAUtils {
    public static KeyPair generateRSAKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048); // 密钥长度，推荐至少2048位
        return keyGen.generateKeyPair();
    }
    public static PublicKey byteArrayToPublicKey(byte[] publicKeyBytes) throws Exception {
        // 创建KeyFactory实例，指定算法为RSA
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        // 创建KeySpec对象，传入公钥的字节数组
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);

        // 使用KeyFactory生成PublicKey
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        return publicKey;
    }

    public static PrivateKey byteArrayToPrivateKey(byte[] privateKeyBytes) throws Exception {
        // 创建KeyFactory实例，指定算法为RSA
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        // 创建KeySpec对象，传入私钥的字节数组
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);

        // 使用KeyFactory生成PrivateKey
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        return privateKey;
    }

    public static byte[] encrypt(PublicKey publicKey, String plainText) throws Exception {
        Cipher encryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return encryptCipher.doFinal(plainText.getBytes("UTF-8"));
    }

    public static String decrypt(PrivateKey privateKey, byte[] encryptedBytes) throws Exception {
        Cipher decriptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        decriptCipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = decriptCipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, "UTF-8");
    }
    public static void main(String[] args) throws Exception {
        // 生成密钥对
        KeyPair keyPair = generateRSAKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 待加密的信息
        String originalText = "Hello, RSA!";
        // 使用Base64编码转换为字符串
        String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
        // 获取PKCS#8编码的私钥字节数组
        String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());;

        // 加密
        byte[] encryptedBytes = encrypt(publicKey, originalText);
        System.out.println("Encrypted: " + Base64.getEncoder().encodeToString(encryptedBytes));

        // 解密
        String decryptedText = decrypt(privateKey, encryptedBytes);
        System.out.println("Decrypted: " + decryptedText);
    }

}
